<?php
namespace Hphp\Event;

use Hphp\Foundation\Application;

class Event
{
    // protected $app;

    protected $events = [];

    // public function __construct(Application $app = null)
    // {
    //     $this->app = $app;
    // }

    /**
     * 事件注册监听
     * @param  string $event    事件标识
     * @param  Closure $callback 事件回调函数
     */
    public function listener($listener, $callback)
    {
        $listener = \strtolower($listener);
        $this->events[$listener] = ['callback' => $callback];
    }

    /**
     * 事件的触发函数
     * @param  string $listener 事件标识
     * @param  array  $param 事件参数
     */
    public function dispatcher($listener, $param = [])
    {
        $listener = \strtolower($listener);

        if (isset($this->events[$listener])) {
            ($this->events[$listener]['callback'])(...$param);
            return true;
        }
    }

    public function getEvents($listener = null)
    {
        return empty($listener) ? $this->events : $this->events[$listener];
    }
}